<?php
namespace app\index\controller;

use think\Controller;
use think\Db;

class Interest extends CommonController
{
    //选择自己的兴趣
    public function select()
    {
        $id = Db::table('user')->where('Number', getCookie())->value('user_id');
        $this->assign('id', $id);
        return $this->fetch('select');
    }

    public function getInterest($userID)
    {
        $select = Db::table('interest')->where('user_id', $userID)->select();
        if (empty($select)) {
            Db::table('interest')->insert(['user_id' => $userID]);
            $select = Db::table('interest')->where('user_id', $userID)->select();
        }
        $uncheck = array_filter($select[0], "unFlterOne");
        $uncheck = array_keys($uncheck);
        $checked = array_filter($select[0], "flterOne");
        $checked = array_keys($checked);
        $uncheck = Db::table('project')->field(['name', 'english_name'])->where('english_name', 'IN', $uncheck)->select();
        $checked = Db::table('project')->field(['name', 'english_name'])->where('english_name', 'IN', $checked)->select();
        $arr = array(
            'succ' => true,
            'state' => '1',
            'data' => array(
                'checked' => $checked,
                'uncheck' => $uncheck,
            ),
        );
        $res = json_encode($arr);
        return $res;
    }

    public function switchInterest($englishName, $flag)
    {
        $user_id = getUserId();
        //return dump($user_id);
        $upSwi = Db::table('interest')->where('user_id', $user_id)->update([$englishName => $flag]);
        return $upSwi;
    }
}
